<?php

declare(strict_types=1);


namespace app\controller\mini;

use app\controller\admin\AdminBaseController;
use app\model\admin\Product;
use app\model\admin\ProductAttrResult;
use app\model\admin\ProductAttrValue;
use app\Request;
use app\model\admin\SysMember;
use app\model\mini\Banner;
use app\model\mini\Cart;
use app\model\mini\Cate;
use app\model\mini\MiniUser;
use app\service\JwtAuth;

class ProductController extends AdminBaseController
{

    public function getBannerList(Request $request)
    {
        $list = Banner::where(['status' => 1])->select();
        return success($list);
    }
    public function getHomeList(Request $request)
    {
        // $user_id = $request->user_id;
        $list = Product::where('status', 1)->order('sort', 'desc')->paginate([
            'list_rows' => $this->limit,
            'page' => $this->page,
            'var_page' => $this->pageVar
        ]);
        return successPage($list);
    }
    public function getProductInfo(Request $request)
    {
        $id = input('id');
        if (!$id) {
            return error('参数错误');
        }
        $info = Product::find($id);
        $info->attr = [];
        $minPrice = 0;
        if ($info->spec_type == 1) {
            // 获取规格
            $attr = ProductAttrResult::where('product_id', $id)->find();
            $info->attr = $attr->result;
            // var_dump($info->attr['value']);
            $minPrice = searchMin($info->attr['value'], 'price');
        } else {
            $minPrice = $info->price;
        }

        // 获取最低价
        $info->minPrice = number_format(floatval($minPrice), 2);

        // $info->attr = $attr->result;
        return success($info);
    }
    public function getSpecDetail(Request $request)
    {
        $product_id = input('product_id');
        $spec = input('spec'); //红色,l
        $info = ProductAttrValue::where('sku', $spec)->where('product_id', $product_id)->find();
        return success($info);
    }
    public function getUserInfo(Request $request)
    {
        $user_id = $request->user_id;
        $info = MiniUser::find($user_id);
        return success($info);
    }
    // 加入购物车
    public function addCart(Request $request)
    {
        $product_id = input('product_id');
        $spec = input('spec'); //红色,l
        $num = input('num'); //红色,l
        $is_show = input('is_show', 1); //1显示0不显示
        $product = Product::find($product_id);
        $user_id = $request->user_id;
        if (!$product) {
            return error('产品不存在');
        }
        $p_a_val = ProductAttrValue::where('product_id', $product_id)->where('sku', $spec)->find();
        if (!$is_show) {
            //不显示，直接点击购买
            if ($product->spec_type == 1) {
                $hidden_cart = Cart::create([
                    "product_id" => $product_id,
                    "num" => $num,
                    "user_id" => $user_id,
                    "unique" => $p_a_val->unique,
                    "sku" => $spec,
                    "spec_type" => $product->spec_type,
                    "is_show" => 0
                ]);
            } else {
                $hidden_cart = Cart::create([
                    "product_id" => $product_id,
                    "num" => $num,
                    "user_id" => $user_id,
                    "spec_type" => $product->spec_type,
                    "is_show" => 0
                ]);
            }
            return success($hidden_cart->id);
        }
        if ($product->spec_type == 1) {
            // 判断是否存在购物车
            $cartInfo = Cart::where([
                "product_id" => $product_id,
                "user_id" => $user_id,
                "unique" => $p_a_val->unique,
                "is_show" => 1
            ])->find();
            if ($cartInfo) {
                // 数量+1
                $cartInfo->num = $cartInfo->num + $num;
                $cartInfo->save();
            } else {
                // storage_log()
                // 创建
                Cart::create([
                    "product_id" => $product_id,
                    "num" => $num,
                    "user_id" => $user_id,
                    "unique" => $p_a_val->unique,
                    "sku" => $spec,
                    "spec_type" => $product->spec_type,
                ]);
            }
        } else {
            $cartInfo = Cart::where([
                "product_id" => $product_id,
                "user_id" => $user_id,
                "is_show" => 1
            ])->find();
            if ($cartInfo) {
                // 数量+1
                $cartInfo->num = $cartInfo->num + $num;
                $cartInfo->save();
            } else {
                // 创建
                Cart::create([
                    "product_id" => $product_id,
                    "num" => $num,
                    "user_id" => $user_id,
                    "spec_type" => $product->spec_type,
                ]);
            }
        }
        return success();
    }
    // 
    public function getCartList(Request $request)
    {
        $user_id = $request->user_id;
        $list = Cart::with(['product'])
            ->where('user_id', $user_id)
            ->where('is_show', 1)->select()->toArray();
        foreach ($list as $key => &$val) {
            if ($val['spec_type'] == 1) {
                // 
                $product_val = ProductAttrValue::where([
                    "product_id" => $val['product_id'],
                    "sku" => $val['sku']
                ])->find();
                $val['price'] = $product_val->price;
                $val['image'] = $product_val->image;
                $val['goods_name'] = $val['product']['goods_name'];
                $val['status'] = $val['product']['status'];
            } else {
                // 单规格
                $val['price'] = $val['product']['price'];
                $val['image'] = $val['product']['image'];
                $val['goods_name'] = $val['product']['goods_name'];
                $val['status'] = $val['product']['status'];
            }
        }
        return success($list);
    }
    public function setCartNum(Request $request)
    {
        $num = input('num');
        $id = input('id');
        $user_id = $request->user_id;
        $info  = Cart::find($id);
        if ($info && $info->user_id == $user_id) {
            if ($num <= 0) {
                // 删除
                Cart::destroy($id);
            } else {
                $info->num = $num;
                $info->save();
            }
            return success();
        } else {
            // 
            return error('操作失败');
        }
    }
    public function delCart(Request $request)
    {
        $user_id = $request->user_id;
        Cart::where('user_id', $user_id)->delete();
        return  success();
    }

    // 商品分类
    public function getCateList(Request $request)
    {
        $user_id = $request->user_id;
        $params = $request->param();
        if ($params['type'] == 1) {
            // 获取一级分类
            $info = Cate::where([
                'status' => 1,
                "pid" => null
            ])->select();
        } else {
            // 获取二级分类
            $info = Cate::where([
                'status' => 1,
            ])->where('pid',  $params['pid'])->select();
        }
        return success($info);
    }
    public function getProductById(Request $request)
    {
        $cate_id = input('id');
        $list = Product::where([
            'status' => 1,
        ])->where('cate_id', $cate_id)->order('sort', 'desc')->paginate([
            'list_rows' => $this->limit,
            'page' => $this->page,
            'var_page' => $this->pageVar
        ]);
        return successPage($list);
        // return;
    }
}
